package com.ccit.controller;
import com.ccit.domain.Attendance;
import com.ccit.domain.AttendanceType;
import com.ccit.domain.PageBean;
import com.ccit.service.AttendanceService;

import com.ccit.utils.Code;
import com.ccit.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@RestController
@RequestMapping("/attendances")
@CrossOrigin(origins = "http://localhost:8080")
@Api(description = "考勤管理")
public class AttendanceController {
    @Autowired
    private AttendanceService attendanceService;

    @PostMapping
    public Result add(@RequestBody AttendanceType attendance) {
        boolean flag = attendanceService.add(attendance);
        return new Result(flag ? Code.SAVE_OK:Code.SAVE_ERR,flag);
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "根据员工ID删除员工信息")
    public Result delete(@PathVariable Integer id) {
        boolean flag = attendanceService.delete(id);
        return new Result(flag ? Code.DELETE_OK:Code.DELETE_ERR,flag);
    }

    @DeleteMapping("/ids")
    @ApiOperation(value = "根据员工ID删除员工信息")
    public Result deleteByIds(@PathVariable List<Integer> ids) {
        boolean flag = attendanceService.deleteByIds(ids);
        return new Result(flag ? Code.DELETE_OK:Code.DELETE_ERR,flag);
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "根据员工ID获取员工出勤记录")
    public Result getById(@PathVariable Integer id) {
        List<Attendance> attendanceList = attendanceService.getById(id);
        Integer code = attendanceList != null ? Code.GET_OK : Code.GET_ERR;
        String msg = attendanceList != null ? "数据查询成功" : "数据查询失败，请重试！";
        return new Result(code,attendanceList,msg);
    }

    @GetMapping("/byDay/{id}")
    @ApiOperation(value = "获取员工近30天出勤记录")
    public Result select(@PathVariable Integer id){
        List<AttendanceType> attendanceList = attendanceService.selectByDays(id);
        Integer code = attendanceList != null ? Code.GET_OK : Code.GET_ERR;
        String msg = attendanceList != null ? "数据查询成功" : "数据查询失败，请重试！";
        return new Result(code,attendanceList,msg);
    }


    @GetMapping
    public Result selectAll(){
        List<Attendance> attendanceList = attendanceService.selectAll();
        Integer code = attendanceList != null ? Code.GET_OK : Code.GET_ERR;
        String msg = attendanceList != null ? "数据查询成功" : "数据查询失败，请重试！";
        return new Result(code,attendanceList,msg);
    }



    @PutMapping
    @ApiOperation(value = "编辑员工信息")
    public Result update(@RequestBody AttendanceType attendance) {
        boolean flag = attendanceService.update(attendance);
        return new Result(flag ? Code.UPDATE_OK:Code.UPDATE_ERR ,flag);
    }


    @GetMapping("/page")
    @ApiOperation(value = "分页查询员工考勤信息")
    public PageBean<Attendance> getAttendance(
            @RequestParam("currentPage") int currentPage,
            @RequestParam("pageSize") int pageSize) {
        //2. 调用service查询
        PageBean<Attendance> pageBean = attendanceService.selectByPage(currentPage,pageSize);
//        System.out.println(pageBean);
        return pageBean;
    }

    @GetMapping("/pageCondition")
    @ApiOperation(value = "多条件分页查询员工考勤信息")
    public PageBean<Attendance> getAttendanceByCondition(
            @RequestParam("currentPage") int currentPage,
            @RequestParam("pageSize") int pageSize,
            @RequestParam(value = "employeeName", required = false) String employeeName,
            @RequestParam(value = "checkIn", required = false) String checkIn,
            @RequestParam(value = "checkOut", required = false) String checkOut) {

        Attendance attendance = new Attendance();
        attendance.setEmployeeName(employeeName);
        attendance.setCheckIn(checkIn);
        attendance.setCheckOut(checkOut);

        // 调用 service 进行模糊查询
        PageBean<Attendance> pageBean = attendanceService.selectByPageAndCondition(currentPage, pageSize, attendance);
        return pageBean;
    }

    @PostMapping("/punch")
    @ApiOperation(value = "打卡")
    public Result punchCard(@RequestBody AttendanceType attendance){

        boolean flag = attendanceService.punchCard(attendance);
        return new Result(flag ? Code.SAVE_OK:Code.SAVE_ERR,flag);

    }


    @GetMapping("/punchRecord")
    @ApiOperation(value = "获取当天打卡记录")
    public AttendanceType getPunchRecord(@RequestParam("employeeId") String employeeId,
                                         @RequestParam("checkDate") String checkDate) {
//        System.out.println("EMP " + employeeId + checkDate );
        int intEmployee = Integer.parseInt(employeeId);
        return attendanceService.getPunchRecord(intEmployee,checkDate);
    }


    @GetMapping("/by_id_date")
    @ApiOperation(value = "记录")
    public Result selectByEmployeeIdAndDate(){
        List<AttendanceType> attendanceList = attendanceService.selectByEmployeeIdAndDate();
        System.out.println(attendanceList);
        Integer code = attendanceList != null ? Code.GET_OK : Code.GET_ERR;
        String msg = attendanceList != null ? "数据查询成功" : "数据查询失败，请重试！";
        return new Result(code,attendanceList,msg);
    }





}
